#!/usr/bin/perl
#
# Hal cdrom interface
# GNU General Public License

package CdromInterface;

use HalInterface;

sub new
{
    my $self = {};

    shift;
    if(@_)
    {
        # cdrom device
        $self->{DEV} = shift;
    }
    else
    {
        # default cdrom device
        $self->{DEV} = '/dev/cdrom';
    }

    die("device not exists, " . $self->{DEV} . "\n") unless(-e $self->{DEV});

    my $hal = HalInterface->new();

    $self->{HAL} = $hal;
    $self->{HAL_DEVICE_STORAGE} = $hal->get_object($hal->find_by_capability_and_property('storage.cdrom', 'block.device', $self->{DEV}));
    $self->{TRAY_OPEN} = 0xFF; # unknown status

    bless($self);

    $self->close_tray();

    return $self;
}

sub get_volume
{
    my $self = shift;
    my $udi = $self->{HAL}->find_by_capability_and_property('volume.disc', 'block.device', $self->{DEV});
    return $udi ? $self->{HAL}->get_object($udi) : 0;
}

sub get_volume_info
{
    my $self = shift;
    my $volume = $self->get_volume();
    return 0 unless($volume);

    my $capacity = $volume->PropertyExists('volume.disc.capacity') ? $volume->GetProperty('volume.disc.capacity') : 0;
    my $type = $volume->PropertyExists('volume.disc.type') ? $volume->GetPropertyString('volume.disc.type') : "unknown";
    my $is_blank = $volume->PropertyExists('volume.disc.is_blank') ? $volume->GetPropertyBoolean('volume.disc.is_blank') : 0;

    return ($capacity, $type, $is_blank);
}

sub is_media_available
{
    my $self = shift;
    return $self->{HAL_DEVICE_STORAGE}->GetPropertyBoolean('storage.removable.media_available');
}

sub get_media_type
{
    my $self = shift;
    my $volume = $self->get_volume();
    return $volume && $volume->PropertyExists('volume.disc.type') ? $volume->GetPropertyString('volume.disc.type') : "";
}

sub get_disc_capacity
{
    my $self = shift;
    my $volume = $self->get_volume();
    return $volume && $volume->PropertyExists('volume.disc.type') ? $volume->GetPropertyInteger('volume.disc.capacity') : 0;
}

sub open_tray
{
    my $self = shift;
    $self->{HAL_DEVICE_STORAGE}->Eject([]);
    $self->{TRAY_OPEN} = 1;
}

sub mount
{
    my $self = shift;
    my $point = shift;
    my $volume = $self->get_volume();
    $volume->Mount(["/mnt/iso iso9660 ro"], [], []) if($volume);
}

sub close_tray
{
    my $self = shift;
    $self->{HAL_DEVICE_STORAGE}->CloseTray([]);
    $self->{TRAY_OPEN} = 0;
}

sub status_string
{
    my $self = shift;
    my $res = "cdrom status: device(" . $self->{DEV} . ")";

    $res .= ", tray("; 
    if($self->{TRAY_OPEN} == 1)
    {
	$res .= "open";
    }
    elsif($self->{TRAY_OPEN} == 0)
    {
	$res .= "close";
    }
    else
    {
	$res .= "unknown";
    }
    $res .= ")";

    my ($capacity, $type, $is_blank) = $self->get_volume_info();

    # have disc
    if($capacity)
    {
	$res .= ", volume(" . $capacity . "," . $type . ",";
	$res .= ($is_blank ? "blank" : "used");
	$res .= ")";
    }
    else
    {
	$res .= ", no disc";
    }

    return $res;
}

return 1;
